package com.zzusedating.androidapp.fragments;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zzusedating.androidapp.R;
import com.zzusedating.androidapp.TweetActivity;
import com.zzusedating.androidapp.adapters.DiscoverAdapter;
import com.zzusedating.androidapp.customlayout.TitleBarLayout;
import com.zzusedating.androidapp.tools.BasicCallback;
import com.zzusedating.androidapp.tools.BasicHandlerCallback;
import com.zzusedating.androidapp.tools.ConstantsTools;
import com.zzusedating.androidapp.tools.MyApplication;
import com.zzusedating.androidapp.model.TweetItem;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class DiscoverFragment extends Fragment {
    private static final String TAG = "DiscoverFragment";
    public static final String MY_TWEET = "myTweet";
    private static final int USER_NOT_LOGIN = -1;
    private static final int GET_DISCOVER_SUCCESS = 1;

    private static final String USER_NOT_LOGIN_ = "用户未登录";

    private ArrayList<TweetItem> items;
    private DiscoverAdapter adapter;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discover_layout, container, false);

        TitleBarLayout titleBarLayout = view.findViewById(R.id.FragDisc_title);
        titleBarLayout.setClickListenerTo(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(getContext(), TweetActivity.class), 1);
            }
        }, TitleBarLayout.TO_ICON);

        items = new ArrayList<>();
        adapter = new DiscoverAdapter(getContext(), items);
        RecyclerView recyclerView = view.findViewById(R.id.FragDisc_Recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);

        MyApplication.getClient().newCall(
                new Request.Builder()
                .get()
                .url(ConstantsTools.GET_TWEET_LIST)
                .build()
        ).enqueue(callback);
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    private Handler handler = new Handler(new BasicHandlerCallback(){
        @Override
        public boolean handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case USER_NOT_LOGIN:
                    MyApplication.Toast(USER_NOT_LOGIN_);
                    break;
                case GET_DISCOVER_SUCCESS:
                    adapter.notifyDataSetChanged();
                    break;
            }
            return true;
        }
    });
    private BasicCallback callback = new BasicCallback(handler) {
        @Override
        public void onResponse(Call call, @NonNull Response response) {
            JSONArray array = (JSONArray) getObject(response);
            if (array != null){
                List<TweetItem> temp = new Gson().fromJson(array.toString(), new TypeToken<List<TweetItem>>(){}.getType());
                items.clear();
                items.addAll(temp);
                handler.sendMessage(ConstantsTools.createMessage(GET_DISCOVER_SUCCESS));
            }
            else
                handler.sendMessage(ConstantsTools.createMessage(getCode()));
        }
    };
}
